package com.zapi.lottery.utils;

import com.zapi.lottery.entity.LotteryPrize;
import com.zapi.lottery.entity.LotteryRecord;
import com.zapi.lottery.entity.LotterySetting;
import com.zapi.lottery.service.LotteryPrizeService;
import com.zapi.lottery.service.LotteryRecordService;
import com.zapi.lottery.service.LotterySettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 抽奖数据工具类
 * 提供数据验证、重置等功能
 * @author: zhonglz
 * @DATE: 2024-06-09
 */
@Component
public class LotteryDataUtils {

    @Autowired
    private LotteryPrizeService lotteryPrizeService;

    @Autowired
    private LotterySettingService lotterySettingService;

    @Autowired
    private LotteryRecordService lotteryRecordService;

    /**
     * 验证抽奖系统数据完整性
     * @return 验证结果
     */
    public Map<String, Object> validateLotteryData() {
        Map<String, Object> result = new HashMap<>();
        boolean isValid = true;
        StringBuilder message = new StringBuilder();

        try {
            // 1. 检查抽奖范围设置
            List<LotteryPrize> prizes = lotteryPrizeService.list();
            if (prizes.isEmpty()) {
                isValid = false;
                message.append("❌ 缺少抽奖范围设置; ");
            } else {
                message.append("✅ 抽奖范围设置正常; ");
            }

            // 2. 检查奖项设置
            List<LotterySetting> settings = lotterySettingService.list();
            if (settings.isEmpty()) {
                isValid = false;
                message.append("❌ 缺少奖项设置; ");
            } else {
                message.append("✅ 奖项设置正常; ");
            }

            // 3. 检查抽奖记录
            List<LotteryRecord> records = lotteryRecordService.list();
            message.append("📊 抽奖记录数量: ").append(records.size()).append("; ");

            // 4. 统计各奖项中奖情况
            Map<String, Long> awardStats = getAwardStatistics();
            message.append("🏆 奖项统计: ").append(awardStats.toString());

        } catch (Exception e) {
            isValid = false;
            message.append("❌ 数据验证异常: ").append(e.getMessage());
        }

        result.put("valid", isValid);
        result.put("message", message.toString());
        return result;
    }

    /**
     * 获取奖项统计信息
     * @return 奖项统计
     */
    public Map<String, Long> getAwardStatistics() {
        Map<String, Long> stats = new HashMap<>();
        
        try {
            List<LotterySetting> settings = lotterySettingService.list();
            List<LotteryRecord> records = lotteryRecordService.list();

            for (LotterySetting setting : settings) {
                String awardCode = setting.getAwardCode();
                long count = records.stream()
                    .filter(record -> record.getPrizeId().equals(setting.getId()))
                    .count();
                stats.put(awardCode, count);
            }
        } catch (Exception e) {
            System.err.println("获取奖项统计失败: " + e.getMessage());
        }

        return stats;
    }

    /**
     * 重置所有抽奖数据
     * @return 重置结果
     */
    public Map<String, Object> resetAllLotteryData() {
        Map<String, Object> result = new HashMap<>();
        
        try {
            // 1. 清空抽奖记录
            lotteryRecordService.remove(null);
            
            // 2. 重置奖项设置状态
            List<LotterySetting> settings = lotterySettingService.list();
            for (LotterySetting setting : settings) {
                setting.setStatus(1); // 重新启用
            }
            lotterySettingService.updateBatchById(settings);

            result.put("success", true);
            result.put("message", "✅ 所有抽奖数据已重置");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "❌ 重置失败: " + e.getMessage());
        }

        return result;
    }

    /**
     * 获取系统状态信息
     * @return 系统状态
     */
    public Map<String, Object> getSystemStatus() {
        Map<String, Object> status = new HashMap<>();
        
        try {
            // 基础数据统计
            long prizeCount = lotteryPrizeService.count();
            long settingCount = lotterySettingService.count();
            long recordCount = lotteryRecordService.count();

            // 奖项统计
            Map<String, Long> awardStats = getAwardStatistics();

            // 系统状态
            status.put("prizeCount", prizeCount);
            status.put("settingCount", settingCount);
            status.put("recordCount", recordCount);
            status.put("awardStats", awardStats);
            status.put("systemReady", prizeCount > 0 && settingCount > 0);

        } catch (Exception e) {
            status.put("error", e.getMessage());
            status.put("systemReady", false);
        }

        return status;
    }
} 